package com.example.tupiancs34;

import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager2.widget.ViewPager2;
import android.content.ContentResolver;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import java.io.InputStream;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";
    private ViewPager2 viewPager;
    private ImagePagerAdapter adapter;
    private TextView pageIndicator;
    private int currentPosition = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initViews();
        setupViewPager();
        handleIntent(getIntent());
        
        // 检查ContentProvider是否自动启动
        checkContentProviderStatus();
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        handleIntent(intent);
    }

    private void handleIntent(Intent intent) {
        String action = intent.getAction();
        String type = intent.getType();

        if (Intent.ACTION_SEND.equals(action) && type != null) {
            if (type.startsWith("image/")) {
                handleSendImage(intent);
            }
        } else if (Intent.ACTION_VIEW.equals(action)) {
            handleViewImage(intent);
        }
    }

    private void handleSendImage(Intent intent) {
        Uri imageUri = intent.getParcelableExtra(Intent.EXTRA_STREAM);
        if (imageUri != null) {
            Toast.makeText(this, "接收到分享的图片: " + imageUri.toString(), Toast.LENGTH_SHORT).show();
            // 这里可以添加处理接收到的图片的逻辑
        }
    }

    private void handleViewImage(Intent intent) {
        Uri imageUri = intent.getData();
        if (imageUri != null) {
            Toast.makeText(this, "查看图片: " + imageUri.toString(), Toast.LENGTH_SHORT).show();
            // 这里可以添加查看图片的逻辑
        }
    }

    private void initViews() {
        viewPager = findViewById(R.id.viewPager);
        pageIndicator = findViewById(R.id.pageIndicator);

    }

    private void setupViewPager() {
        adapter = new ImagePagerAdapter(this);
        viewPager.setAdapter(adapter);

        // 设置页面变化监听器
        viewPager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                currentPosition = position;
                updatePageIndicator(position);
            }
        });

        // 初始显示第一页
        updatePageIndicator(0);
    }

    private void updatePageIndicator(int position) {
        pageIndicator.setText(String.format("%d / %d", position + 1, adapter.getItemCount()));
    }

    private void shareCurrentImage() {
        if (adapter != null && currentPosition < adapter.getItemCount()) {
            String imageFileName = adapter.getImageFileName(currentPosition);
            if (imageFileName != null) {
                ImageShareHelper.shareImage(this, imageFileName);
            } else {
                Toast.makeText(this, "无法获取当前图片信息", Toast.LENGTH_SHORT).show();
            }
        }
    }

    /**
     * 检查ContentProvider是否自动启动
     */
    private void checkContentProviderStatus() {
        Log.d(TAG, "检查ContentProvider状态...");
        
        try {
            Uri baseUri = Uri.parse("content://com.example.tupiancs34.provider/images");
            ContentResolver resolver = getContentResolver();
            
            // 尝试查询ContentProvider
            android.database.Cursor cursor = resolver.query(baseUri, null, null, null, null);
            if (cursor != null) {
                int count = cursor.getCount();
                Log.d(TAG, "ContentProvider已自动启动！发现 " + count + " 张图片");
                Toast.makeText(this, "ContentProvider已自动启动，发现 " + count + " 张图片", Toast.LENGTH_SHORT).show();
                cursor.close();
            } else {
                Log.e(TAG, "ContentProvider未启动或查询失败");
                Toast.makeText(this, "ContentProvider未启动", Toast.LENGTH_SHORT).show();
            }
        } catch (Exception e) {
            Log.e(TAG, "检查ContentProvider状态时出错", e);
            Toast.makeText(this, "ContentProvider检查失败: " + e.getMessage(), Toast.LENGTH_LONG).show();
        }
    }

    /**
     * 测试ContentProvider功能
     */
    private void testContentProvider() {
        Log.d(TAG, "开始测试ContentProvider...");
        
        // 测试ContentProvider是否可以被其他应用访问

    }
    

}